package com.example.demo.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;


/**
 * 平台所以配置的对外接口
 * @author zhangxiaoyun
 *
 */
public class BaseConfig {
	private static BaseConfig baseConfig = null;
	private static Properties confPro;

	//构造方法私有化 
	private BaseConfig(String filename) {
		FileInputStream inStream = null;
		try {
			if(null == filename){
				filename = this.getClass().getResource("/").getPath()+"/"+"resources/conf/baseConf.properties";	
			}
			inStream = new FileInputStream(new File(filename));
			confPro= new Properties();
			//解决中文乱码 by csl 20171019
			confPro.load(new InputStreamReader(inStream,"UTF-8"));
			//打印参数列表
			System.out.println("confPro={"+confPro+"},filename={"+filename+"}");
		} catch (IOException e) {
			confPro= new Properties();//用于cashier调用
			System.out.println("FileNotFound filename=" + filename+e);
		} finally {
			try {
				inStream.close();
			} catch (Exception e2) {
				System.out.println("close FileInputStream error"+ e2);
			}
		}
	}
	
	// 单例模式获取对象
	/**
	 * 配置文件路径
	 * @param filePath
	 * @return
	 */
	public static BaseConfig getInstence(String filePath) {
		if (baseConfig == null) {
			synchronized (BaseConfig.class) {
				if (baseConfig == null) {
					baseConfig = new BaseConfig(filePath);
				}
			}
			
		}
		return baseConfig;
	}
	public static BaseConfig getInstence() {
		if (baseConfig == null) {
			synchronized (BaseConfig.class) {
				if (baseConfig == null) {
					baseConfig = new BaseConfig(null);
				}
			}
		}
		return baseConfig;
	}

	//获取系统配置属性
	public static String getConfig(String key) {
		return confPro.getProperty(key);
	}
	
	public static void setConfig(String key,String value){
		confPro.setProperty(key, value);
	}
}
